package sdk;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import model.DeviceState;

public class DeviceStateSDK {
    private Client client;

    public DeviceStateSDK(Client client) {
        this.client = client;
    }

    public Map<String, Object> deviceStateRestart(Object req) {
        String appId = client.getAppId();
        String appSecret = client.getAppSecret();
        BuildClientSDK buildClient = new BuildClientSDK(appId,appSecret);
        Map<String, Object> combinedParams = new HashMap<>();
        if (req != null) {
            Class<?> clazz = req.getClass();
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                try {
                    String fieldName = field.getName();
                    Object fieldValue = field.get(req);
                    combinedParams.put(fieldName, fieldValue);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Failed to access field in deviceStateRestart ", e);
                }
            }
        }

        Map<String, Object> postParams = buildClient.buildPostParam(combinedParams);
        return client.post("/open/device/state/restart", postParams);
    }

    public Map<String, Object> deviceStateOffline(Object req) {
        String appId = client.getAppId();
        String appSecret = client.getAppSecret();
        BuildClientSDK buildClient = new BuildClientSDK(appId,appSecret);
        Map<String, Object> combinedParams = new HashMap<>();
        if (req != null) {
            Class<?> clazz = req.getClass();
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                try {
                    String fieldName = field.getName();
                    Object fieldValue = field.get(req);
                    combinedParams.put(fieldName, fieldValue);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Failed to access field in deviceStateOffline ", e);
                }
            }
        }
        Map<String, Object> postParams = buildClient.buildPostParam(combinedParams);
        return client.post("/open/device/state/offline", postParams);
    }

    public Map<String, Object> deviceStateOfflineProfit(Object req) {
        String appId = client.getAppId();
        String appSecret = client.getAppSecret();
        BuildClientSDK buildClient = new BuildClientSDK(appId,appSecret);
        Map<String, Object> combinedParams = new HashMap<>();
        if (req != null) {
            Class<?> clazz = req.getClass();
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                try {
                    String fieldName = field.getName();
                    Object fieldValue = field.get(req);
                    combinedParams.put(fieldName, fieldValue);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Failed to access field in deviceStateOfflineProfit ", e);
                }
            }
        }
        Map<String, Object> postParams = buildClient.buildPostParam(combinedParams);
        return client.post("/open/device/state/offline_profit", postParams);
    }

    public Map<String, Object> deviceStatePortError(Object req) {
        String appId = client.getAppId();
        String appSecret = client.getAppSecret();
        BuildClientSDK buildClient = new BuildClientSDK(appId,appSecret);
        Map<String, Object> combinedParams = new HashMap<>();
                if (req != null) {
            Class<?> clazz = req.getClass();
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                try {
                    String fieldName = field.getName();
                    Object fieldValue = field.get(req);
                    combinedParams.put(fieldName, fieldValue);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Failed to access field in deviceStatePortError ", e);
                }
            }
        }
        Map<String, Object> postParams = buildClient.buildPostParam(combinedParams);
        return client.post("/open/device/state/port/error", postParams);
    }

    public Map<String, Object> deviceState(DeviceState.DeviceStateReq req) {
        String appId = client.getAppId();
        String appSecret = client.getAppSecret();
        BuildClientSDK buildClient = new BuildClientSDK(appId,appSecret);
        Map<String, Object> getParams = new TreeMap<>();
        getParams.put("device_num", req.device_num); 
        getParams.put("port_fields", req.port_fields != null ? String.join(",", req.port_fields) : null); 
        Map<String, Object> getParamsf = buildClient.buildGetParam(getParams);
        return client.get("/open/device/state", getParamsf);
    }
}
